﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;
using Avatar.MembershipService;

namespace Avatar.Management
{
    public static class MenuManager
    {
        /// <summary>
        /// 添加管理单元
        /// </summary>
        /// <param name="_name"></param>
        /// <returns></returns>
        public static bool DepartmentAdd(string _name)
        {
            if (string.IsNullOrEmpty(_name)) throw new Exception("管理单元名称不能为空");
            if (_name.Length > 10) throw new Exception("管理单元名称不能超过10个字符");

            var _order = DBHelper.GetAll<Management_Department>("获取管理单元列表出错:").Max(d => d.Order);
            Management_Department _department = new Management_Department();
            _department.Name = _name;
            _department.Order = _order + 10;

            return DBHelper.Insert(_department, d => d.Name == _name, "添加管理单元出错：");
        }
        /// <summary>
        /// 编辑后台菜单分类
        /// </summary>
        /// <param name="_id"></param>
        /// <param name="_name"></param>
        /// <returns></returns>
        public static void DepartmentUpdate(int _id, string _name, int _order, bool _lock)
        {
            try
            {
                if (_id == 0) throw new Exception("管理单元ID不能为0");
                if (string.IsNullOrEmpty(_name)) throw new Exception("管理单元名称不能为空");
                if (_name.Length > 10) throw new Exception("管理单元名称不能超过10个字符");
                if (_order == 0) throw new Exception("排序ID不能为0");

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _d = _adc.Management_Department.Where(d => d.ID == _id).FirstOrDefault();
                    if (_d != null)
                    {
                        _d.Name = _name;
                        _d.Order = _order;
                        _d.Open = _lock;
                    }
                    else
                    {
                        throw new Exception("菜单分类不存在");
                    }
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("编辑系统后台菜单分类出错：" + _e.Message);
            }
        }
        /// <summary>
        /// 删除管理单元
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static void DepartmentDelete(int _id)
        {
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _department = _adc.Management_Department.Where(d => d.ID == _id).FirstOrDefault();

                    if (_department == null) throw new Exception("菜单分类不存在");
                    if (_department.Management_Category.Count() > 0) throw new Exception("此菜单分类下面包含子菜单，请先删除子菜单，否则不能删除。");

                    _adc.Management_Department.DeleteOnSubmit(_department);

                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("删除系统后台菜单出错：" + _e.Message);
            }
        }
        /// <summary>
        /// 根据ID获取管理单元
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static Management_Department GetDepartment(int _id)
        {
            var _department = DBHelper.Get<Management_Department>(d => d.ID == _id, "获取管理单元出错：");
            if (_department == null) throw new Exception("管理单元不存在");

            return _department;
        }
        /// <summary>
        /// 获取管理单元列表
        /// </summary>
        /// <returns></returns>
        public static List<Management_Department> GetDepartment()
        {
            return DBHelper.GetAll<Management_Department>(d => d.Order, "获取管理单元列表出错：");
        }

        /// <summary>
        /// 获取管理项目列表
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static List<Management_Category> GetCategories(int _id)
        {
            return DBHelper.GetAll<Management_Category>(c => c.DepartmentID == _id, c => c.Order, "获取管理项目列表失败：");
        }
        /// <summary>
        /// 根据ID获取管理项目
        /// </summary>
        /// <param name="_id"></param>
        /// <returns></returns>
        public static Management_Category GetCategory(int _id)
        {
            return DBHelper.Get<Management_Category>(c => c.ID == _id, "获取管理项目出错：");
        }
        public static Management_Category GetCategory(string _fileName)
        {
            return DBHelper.Get<Management_Category>(c => c.Url == _fileName, "获取管理项目出错：");
        }
        public static bool CategoryAdd(string _name, int _did, string _url, string _description)
        {
            if (_did == 0) throw new Exception("管理单元ID不能为空");
            if (string.IsNullOrEmpty(_name)) throw new Exception("名称不能为空");
            if (string.IsNullOrEmpty(_url)) throw new Exception("URL地址不能为空");
            if (_name.Length > 10) throw new Exception("名称不能超过10个字符");
            if (_url.Length > 50) throw new Exception("URL地址不能超过10个字符");
            if (!string.IsNullOrEmpty(_description) && _description.Length > 50) throw new Exception("说明文字不能超过50个字");

            var _list = DBHelper.GetAll<Management_Category>(o => o.DepartmentID == _did, "获取管理项目列表出错:");
            int _order = 0;
            if (_list.Count() > 0)
                _order = _list.Max(o => o.Order) + 10;

            Management_Category _category = new Management_Category();
            _category.Name = _name;
            _category.DepartmentID = _did;
            _category.Url = _url;
            _category.Description = _description;
            _category.Order = _order;
            _category.Open = false;
            return DBHelper.Insert(_category, c => ((c.Name == _name) || (c.Url == _url)) && c.DepartmentID == _did, "添加管理项目出错：");
        }
        /// <summary>
        /// 编辑系统后台菜单
        /// </summary>
        /// <param name="_cid"></param>
        /// <param name="_name"></param>
        /// <param name="_did"></param>
        /// <param name="_url"></param>
        /// <param name="_description"></param>
        /// <param name="_order"></param>
        /// <param name="_lock"></param>
        public static void CategoryUpdate(int _cid, string _name, int _did, string _url, string _description, int _order, bool _lock)
        {
            try
            {
                if (_did == 0) throw new Exception("管理单元ID不能为空");
                if (string.IsNullOrEmpty(_name)) throw new Exception("名称不能为空");
                if (string.IsNullOrEmpty(_url)) throw new Exception("URL地址不能为空");
                if (_name.Length > 10) throw new Exception("管理项目名称不能超过10个字符");
                if (!string.IsNullOrEmpty(_description) && _description.Length > 50) throw new Exception("说明文字不能超过50个字");
                if (_order == 0) throw new Exception("排序号码不能为零");

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _category = _adc.Management_Category.Where(c => c.ID == _cid).FirstOrDefault();
                    
                    if (_category == null)throw new Exception("系统菜单分类不存在");

                    _category.Name = _name;
                    _category.Url = _url;
                    _category.Order = _order;
                    _category.Open = _lock;
                    _category.Description = _description;
                    
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("编辑系统后台菜单出错：" + _e.Message);
            }
        }
        public static void CategoryDelete(int _categoryID)
        {
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _category = _adc.Management_Category.Where(c => c.ID == _categoryID).FirstOrDefault();

                    if (_category == null) throw new Exception("菜单不存在");

                    foreach (var _r in _category.AdministratorAccessRole)
                    {
                        _adc.AdministratorAccessRole.DeleteOnSubmit(_r);
                    }

                    _adc.Management_Category.DeleteOnSubmit(_category);

                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("删除菜单出错:" + _e.Message);
            }
        }

    }
}
